package com.mnt.washio.utils.network.ws;

import java.io.IOException;
import java.util.HashMap;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;

import com.mnt.washio.utils.Statics;
import com.mnt.washio.utils.network.NetworkUtils;
import com.mnt.washio.utils.network.WebServiceCommunicatorListener;
import com.parse.ParseUser;

@SuppressLint("SimpleDateFormat")
public class PlaceOrderWSControl extends BaseWSControl {
	public static boolean CREATE_ADDRESS = true;
	public static boolean UPDATE_ADDRESS = false;

	private String userId;
	private String addressId;
	private String zipCode;
	private String stripCardId;
	private String pickUpDate;
	private String pickUpTime;
	private String dropOffDate;
	private String dropOffTime;
	private String nanigansTrackingId;
	private boolean isFreeAndClear;
	private String display;
	private boolean flag;
	private String notes;

	public PlaceOrderWSControl(Context context,
			WebServiceCommunicatorListener listener, String addressId,
			String zipCode, String stripCardId, String pickUpDate,
			String pickUpTime, String dropOffDate, String drofOffTime,
			String nanigansTrackingId, boolean isFreeAndClear, String display,
			String notes) {
		super(context, listener, WebServiceFlag.PLACE_ORDER);
		this.userId = ParseUser.getCurrentUser().getObjectId();
		this.addressId = addressId;
		this.zipCode = zipCode;
		this.stripCardId = stripCardId;
		this.pickUpDate = pickUpDate;
		this.pickUpTime = pickUpTime;
		this.dropOffDate = dropOffDate;
		this.dropOffTime = drofOffTime;
		this.nanigansTrackingId = nanigansTrackingId;
		this.isFreeAndClear = isFreeAndClear;
		this.display = display;
		this.notes = notes;
	}

	@Override
	protected Object doInBackground(Void... ps) {
		// Get local database
		String url = Statics.ADMIN_WS_URL + "orders/";
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("addressId", addressId);
		params.put("zip", this.zipCode);
		params.put("stripeCardId", this.stripCardId);
		params.put("pickupDateTime", this.pickUpDate);
		params.put("pickupTime", this.pickUpTime);
		params.put("dropOffDateTime", this.dropOffDate);
		params.put("dropOffTime", this.dropOffTime);
		params.put("notes", this.notes);
		params.put("nanigansTrackingId", this.nanigansTrackingId);
		params.put("isFreeAndClear", this.isFreeAndClear ? "1" : "0");
		params.put("display", this.display);

		try {

			String respone = NetworkUtils.doPostRequest(url, params);

			try {
				JSONObject jsonObject = new JSONObject(respone);

				return jsonObject.getString("error");
			} catch (JSONException e) {
				return "Successful";
			}

		} catch (ClientProtocolException e) {
			mError = "Cannot connect to network";
			e.printStackTrace();
		} catch (IOException e) {
			mError = "Cannot connect to network";
			e.printStackTrace();
		}
		return mError;
	}

}
